<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\BatchISBN;
use App\Admin\Actions\Post\Collection;
use App\Admin\Actions\Post\JoinPrimary;
use App\Admin\Actions\Post\RouteAction;
use App\Http\Controllers\Controller;
use App\Models\Goods;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Zhusaidong\GridExporter\Exporter;

class GoodController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('商品列表')
			->description('已销售商品记录')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('商品详情')
			->description('销售商品详细信息')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('修改')
			->description('修改商品信息')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('Create')
			->description('description')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$_scope_ = request()->get('_scope_');
		$isbn = request()->get('isbn');
		$grid = new Grid(new Goods);
		$grid->batchActions(function ($batch) {
			$batch->add(new BatchISBN());
			$batch->add(new JoinPrimary());
		});
		$grid->tools(
			function ($tools) {
				$tools->batch(
					function ($batch) {
						$batch->disableDelete();
					}
				);
			}
		);
		$grid->actions(function ($actions) {

			$actions->disableDelete();

			$actions->add(new RouteAction(2));
		});

		if ($_scope_ == 'isbnBook') {
			$grid->model()->selectRaw("*,count(isbn) as num")->groupBy('isbn')->orderBy('num', 'desc');
		} else {
			$grid->model()->orderBy('date', 'desc');
		}

		// if (!empty($isbn)) {
		//     $grid->model()->selectRaw("*,count(title) as num")->groupBy('title')->orderBy('num', 'desc');
		// }

		$grid->id('ID')->display(function ($id) {
			$url = route('goods.index') . '?title=' . $this->title;
			return "<a target='_blank'  href='" . $url . "'>" . $id . "</a>";
		})->sortable();
		$grid->item_id('商品ID');
		$grid->isbn('ISBN')->editable()->sortable();
		$grid->title('商品名')->editable();
		$grid->img_url('图片')->image(100, 100)->modal(function ($modal) {
			$big_img = str_replace("_s", "", $this->img_url);
			return "<img style='width:100%;' src='" . $big_img . "'>";
		});
		if ($_scope_ == 'isbnBook' || !empty($isbn)) {
			$grid->num('销量');
		}

		$grid->add_time('上架时间');
		$grid->price('销售价格')->display(function ($price) {
			$url = "https://search.douban.com/book/subject_search?search_text=" . $this->title;
			return "<a target='_blank'  href='" . $url . "'>" . $price . "</a>";
		});
		$grid->date('销售时间');
		$grid->areaname('销售地区');
		$grid->pinxiang('品相');
		$grid->press('出版社');
		$grid->pubdate('出版时间');
		$grid->shop_id('店铺ID')->display(function ($shop_id) {
			return "<a target='_blank'  href='http://shop.kongfz.com/" . $shop_id . "'>" . $shop_id . "</a>";
		});
		$grid->column('url', '商品地址')->link();
		$grid->column('star')->action(Collection::class);
		$grid->rate('评分');
		$grid->created_at('创建时间')->sortable();
		$grid->filter(function ($filter) {

			$filter->disableIdFilter();
			$filter->equal('item_id', '商品ID');
			$filter->equal('isbn', 'ISBN');
			$filter->like('title', '商品名');
			$filter->equal('shop_id', '店铺ID');
			$filter->between('price', '价格');
			$filter->scope('isbnBook', 'ISBN图书')->where('isbn', '!=', '');

		});
		$exporter = Exporter::get($grid);
		$exporter->setFileName('商品列表.xlsx');
		$exporter->setExclusions(['img_url', 'rate', 'star', 'url']);

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(Goods::findOrFail($id));
		$show->panel()
			->tools(function ($tools) {
				$tools->disableDelete();
			});

		$show->id('ID');
		$show->item_id('商品ID');
		$show->isbn('ISBN');
		$show->title('商品名');
		$show->price('成交价格');
		$show->pinxiang('品相');
		$show->date('成交时间');
		$show->shop_name('店铺名');
		$show->url('孔网成交地址')->link();
		$show->shop_id('孔网店铺地址')->as(function ($shop_id) {
			return "http://shop.kongfz.com/" . $shop_id;
		})->link();
		$show->cloumn('多抓鱼搜索')->as(function () {
			return "https://www.duozhuayu.com/search/" . $this->title;
		})->link();
		$show->img_url('商品图片')->unescape()->as(function ($img) {
			return "<img src='{$img}' />";
		});
		$show->date('销售时间');
		$show->areaname('销售地区');
		$show->star('收藏')->as(function ($star) {
			return $star ? '已收藏' : '未收藏';
		});

		$show->rate('评分');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new Goods);
		$form->tools(function ($tools) {
			$tools->disableDelete();
		});
		$form->display('id');
		$form->text('title', '商品名');
		$form->text('isbn', 'ISBN');
		$form->radio('star', '收藏')->options(['0' => '未收藏', '1' => '已收藏']);
		$form->display('price', '成交价格');
		$form->display('pinxiang', '品相');
		$form->display('date', '成交时间');
		$form->display('url', '孔网成交地址');
		$form->file('img_url', '商品图片');
		$form->starRating('rate', '商品评分');
		return $form;
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param int $id
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function update($id) {
		$name = request()->post('name');
		$value = request()->post('value');
		if ($name) {
			$model = Goods::find($id);
			$model->remark = 0;
			$model->save();
		}

		return $this->form()->update($id);
	}
}
